using FPLibrary;
using Pvp.Ecs;
using UnityEngine;

namespace Pvp
{
    /// <summary>
    /// 通过特殊事件来判定来破坏路障
    /// </summary>
    [EntityDeclare((int)EntityType.gear, (int) GearType.可破坏路障2)]
    class EntityDeclare1644 :BaseEntityDeclare
    {
        private ISpecialEvent m_Event;
        private int destroyEventName = 10;
        public override void OnEntityCreate(Entity entity,object userData)
        {
            entity.AddComponent<IUnit>();
            entity.AddComponent<IView>();
            m_Event = entity.AddComponent<ISpecialEvent>();
            var gear =  entity.AddComponent<IGearUnit>();
            gear.GearDestroyTrigger = OnDestroy;
        }

        private bool OnDestroy(IGearUnit gear)
        {
            return m_Event.eventName == destroyEventName;
        }
    }

    [EntityDeclare((int)EntityType.monster, (int) MonsterType.怪物1)]
    class EntityDeclare1413 : BaseEntityDeclare
    {
        private IUnit m_Unit;
        private IAnimator m_Animator;
        private ILifeUnit m_LifeUnit;
        public override void OnEntityCreate(Entity entity,object userData)
        {
            m_Unit = entity.AddComponent<IUnit>();
            entity.AddComponent<IView>();
            entity.AddComponent<IHurtBox>();
            m_LifeUnit = entity.AddComponent<ILifeUnit>();
            m_Animator = entity.AddComponent<IAnimator>();
            var controller = entity.AddComponent<IController>();
            controller.MoveAction = OnUnitMove;
            controller.SkillAction = OnSkill;
        }

        private void OnUnitMove()
        {
            if (m_LifeUnit.LifeValue <= 0)
                return;
        }

        private void OnSkill()
        {
            if (m_LifeUnit.LifeValue <= 0)
            {
                m_Animator.animStateName = "deadState";
                return;
            }
        }
        
        public void Clone()
        {}
    }

    [EntityDeclare((int)EntityType.hero, (int) HeroType.男)]
    class EntityDeclare1446 : BaseEntityDeclare
    {
        private IUnit m_Unit;
        private IMove m_Move;
        private ILifeUnit m_LifeUnit;
        private IPlayerInput m_Input;
        private IController m_Controller;
        private Fix64 moveSpeed = 0.1f;
        
        public override void OnEntityCreate(Entity entity,object userData)
        {
            m_Unit = entity.AddComponent<IUnit>();
            entity.AddComponent<IView>();
            entity.AddComponent<IHurtBox>();
            m_Move = entity.AddComponent<IMove>();
            m_LifeUnit = entity.AddComponent<ILifeUnit>();
            m_Controller = entity.AddComponent<IController>();
            m_Controller.MoveAction = OnUnitMove;
            m_Controller.SkillAction = OnSkill;
            m_Controller.ControllerId = (int)userData;
            
            Debug.Log("contoooid = "+ m_Controller.ControllerId);

            m_Input = Entities.GetSingleComponent<IPlayerInput>();
        }

        private void OnUnitMove()
        {
            Debug.Log($"on move func :{m_Input.inputPlayerId},{m_Controller.ControllerId}");
            if (m_Input.inputPlayerId != m_Controller.ControllerId)
                return;
            if (m_LifeUnit.LifeValue <= 0)
                return;
            if (m_Input.moveDir == FPVector2.zero)
            {
                m_Move.MoveSpeed = 0;
                return;
            }

            m_Move.Forward = m_Input.moveDir;
            m_Move.MoveSpeed = moveSpeed;
        }


        private void OnSkill()
        {
            if (m_Input.inputPlayerId != m_Controller.ControllerId)
                return;
            if (m_Input.moveDir == FPVector2.zero)
            {
                return;
            }
        }
    }
}